<template>
  <div class="container-big">
    <div class="container-left">
      <ul class="tablist">
        <li :class="active==0?`active`:``" @click="getAcitve(0)">企微标签</li>
        <li :class="active==1?`active`:``" @click="getAcitve(1)">个人标签</li>
      </ul>
      <template v-if="active==0">
      <div class="line"></div>
        <div class="search">
          <el-input v-model="queryParams.userName1" size="small" placeholder="搜索标签" clearable/>
          <el-button icon="el-icon-search" size="small" @click="handleQuery"></el-button>
        </div>
        <el-tree
            v-loading="loading"
            ref="draggtree"
            :data="data"
            :props="defaultProps"
            node-key="id"
            default-expand-all
            @node-drag-start="handleDragStart"
            @node-drop="handleDrop"
            :draggable="true"
            :allow-drop="allowDrop"
            :allow-drag="allowDrag"
            class="dragtree"
        >
          <div
              class="role_item commhover flexbetween f-14"
              slot-scope="{ node, data }"
              :class="{ active: activeId == data.id }"
              @click="handleItem(data,node)"
          >
            <div class="flex aligncenter w_full">
              <svg-icon class-name="mr-26 draggericon f-14 c_text2" icon-class="sort" />
              <div class="role-title c_text2 f-13">
                <svg-icon v-if="node.level==1" icon-class='i_file' class-name='filename mr-3' style="color:#b7b7b7" />
                {{ data.label }}
                <span v-if="node.level!==1" class="c_text3 f-12">（3）</span>
              </div>
            </div>
          </div>
        </el-tree>
      </template>
    </div>
    <div class="container-right">
      <div class="content-big"  v-if="active==0">
        <div class="content-tabel">
          <div class="content-head">
            <div class="content-left">
              <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="130px">
                <el-button size="small" @click="handleAdd" v-hasPermi="['companyLabelList:add']">添加客户</el-button>
                <el-form-item prop="userName1">
                  <div class="search">
                    <el-input v-model="queryParams.userName1" placeholder="搜索客户昵称备注" clearable/>
                    <el-button icon="el-icon-search" size="small" @click="handleQuery"></el-button>
                  </div>
                </el-form-item>
                <el-form-item prop="userName1">
                  <el-select v-model="queryParams.id" placeholder="请选择">
                    <el-option
                      v-for="item in userList"
                      :key="Number(item.value)"
                      :label="item.label"
                      :value="Number(item.value)">
                      <span style="float: left">{{ item.label }}</span>
                      <span style="float: right; color: #8492a6; font-size: 13px">{{ item.value }}</span>
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-form>
            </div>
            <div class="content-right">
              <div class="group-btn">
                <el-button size="small" @click="handleHistoricalTags" v-hasPermi="['companyLabelList:history']">历史标签人物</el-button>
                <!--                <el-button size="medium" @click="handleCustom">自定义表格</el-button>-->
              </div>
            </div>
          </div>
          <el-table
            :data="tableDataList"
            ref="companylabelList"
            @selection-change="handleSelectionChange"
          >
            <el-table-column
              :label="item.label"
              align="center"
              v-for="(item, index) in tableColumns"
              :key="index"
              :prop="item.key"
              :min-width="item.minWidth ? item.minWidth : ``"
              :fixed="item.fixed ? item.fixed : false"
              :show-overflow-tooltip="true"
            >
              <!--用于表格自定义-->
              <template slot-scope="scope">
                <div v-if="item.key=='name'">
                  <div class="userInfo">
                    <img src="../../../assets/images/profile.jpg"/>
                    <div class="name">小城红</div>
                    <div class="label">@志新</div>
                  </div>
                </div>
                <div class="label-list" v-else-if="item.key=='label'">
                  <label>企业：</label>
                  <span>未入VIP群</span>
                  <span>持续7天未发言</span>
                  <span>1000</span>
                </div>

                <div v-else style="text-align: center">{{ scope.row[item.key] }}</div>
              </template>
            </el-table-column>
          </el-table>
          <pagination
            v-show="total>0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
          />
        </div>
        <div class="table-footer">
          <div class="footer-left">
            <div class="chooseAll">选择全部</div>
            已选<span>13</span>个
          </div>
          <div class="footer-right">
            <el-button type="primary" plain size="small" v-hasPermi="['companyLabelList:moveLabel']">移动到其他标签</el-button>
            <el-button type="primary" plain size="small" v-hasPermi="['companyLabelList:newLabel']">打上新标签</el-button>
            <el-button type="primary" plain size="small" v-hasPermi="['companyLabelList:delLabel']">从标签里删除</el-button>
          </div>
        </div>
      </div>
      <div class="gotoPage" v-if="active==1">
        个人标签隶属于单个企微号，如需查看/管理个人标签，请切换至单号模式下操作
        <el-button type="primary " @click="goto('/single/customerOperationsMgt/SingleCustomerLabelList?active=0')">去使用</el-button>
      </div>
    </div>
    <selectPeople v-if="showSelectPeople" :visableIf.sync="showSelectPeople"></selectPeople>
    <echartData v-if="showEechartData" :visableIf.sync="showEechartData"></echartData>
  </div>
</template>

<script>
import {listUser, delUser,} from "@/api/system/user";

export default {
  name: "companyLabelList",
  components: {
    'selectPeople': () => import('./selectPeople.vue'),//导出弹窗
    'echartData': () => import('./echartData.vue'),//历史标签
  },
  data() {
    return {
      showEechartData: false,
      showSelectPeople: false,
      active: 0,
      activeId:-1,//默认为全部
      loading: false,
      userList: [
        {
          value: 1,
          label: '锐行',
          name: '小微',
        },
        {
          value: 2,
          label: '锐行',
          name: '小红',
        },
      ],
      data: [{
        label: '一级 1',
        id:1,
        children: [{
          label: '二级 1-1',
          id:2,
        }]
      }, {
        label: '一级 2',
        id:3,
        children: [{
          label: '二级 2-1',
          id:4,
        }, {
          label: '二级 2-2',
          id:5,
        }]
      },],
      defaultProps: {
        children: 'children',
        label: 'label'
      },
      showExportPage: false,//导出
      tableHeight: 600,
      tableColumns: [
        {key: 'name', label: `客户`, visible: true, minWidth: 150, fixed: true},
        {key: 'label', label: `标签`, visible: true, minWidth: 150, fixed: false},

      ],
      tableDataList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        id: null,//用户id
        userName1: null,

      },
      total: 8, // 表格的总条数
    };
  },
  created() {
    // this.getList();
    this.tableDataList = [];
    for (let i = 0; i < 10; i++) {
      let obj = {
        name: '小红',
        label: '18207418337',
        operate: true,
      }
      this.tableDataList.push(obj);
    }
  },
  methods: {
    goto(path){
      this.$router.push({
        path:path,
      })
    },
    getAcitve(index) {
      this.active = index;
    },
    handleDragEnd(draggingNode, dropNode, dropType, ev) {
      console.log('tree drag end: ', dropNode && dropNode.label, dropType);
    },
    handleAdd() {
      this.showSelectPeople = true;
    },
    handleNodeClick(data) {
      console.log(data);
    },
    showLabel() {
      this.showSelectLabelPage = true;
    },
    /** 查询用户列表 */
    getList() {
      this.loading = true;
      listUser(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          this.tableDataList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.queryParams.deptId = undefined;
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(val) {
      this.multipleSelection = [];
      if (val && val.length > 0) {
        val.forEach((item) => {
          this.multipleSelection.push(item.id);
        })
        console.log('multipleSelection', this.multipleSelection);
      }
    },
    handleView(row) {
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      // const userIds = row.userId || this.ids;
      // this.$modal.confirm('是否确认删除用户编号为"' + userIds + '"的数据项？').then(function () {
      //   return delUser(userIds);
      // }).then(() => {
      //   this.getList();
      //   this.$modal.msgSuccess("删除成功");
      // }).catch(() => {
      // });
      this.$msgBox.showMsgBox({
        title: "是否确认删除？",
        content: "该企微号正在执行服务，删除将会使该号从操作中释放，导致部分功能无法正常使用，请谨慎选择。*如该号已加入在线聊天，成功删除后，聊天记录会被一并删除，重新登录后无法恢复。",
      })
        .then(async (val) => {
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    handleNumberDelete() {
      // const userIds = row.userId || this.ids;
      // this.$modal.confirm('是否确认删除用户编号为"' + userIds + '"的数据项？').then(function () {
      //   return delUser(userIds);
      // }).then(() => {
      //   this.getList();
      //   this.$modal.msgSuccess("删除成功");
      // }).catch(() => {
      // });
      this.$msgBox.showMsgBox({
        title: "是否确认删除？",
        content: "该企微号正在执行服务，删除将会使该号从操作中释放，导致部分功能无法正常使用，请谨慎选择。*如该号已加入在线聊天，成功删除后，聊天记录会被一并删除，重新登录后无法恢复。",
      })
        .then(async (val) => {
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.showExportPage = true;
    },
    handleCustom() {
      this.showCustomListPage = true;
    },
    handerTableLabel() {
      console.log('点击了打开标签',);
      this.showTableLabel = true;
    },
    handleSelectSmartTags() {
      //打开智能标签
      this.showSelectSmartTags = true;
    },
    handleHistoricalTags() {
      this.showEechartData = true;
    },

    handleDragStart(node, ev) {
      console.log("drag start", node);
      this.draggingNode = node;
    },
    handleItem(item,node={}) {

      if(node.level == 1) return
      console.log(item,node)
      this.activeId = item.id;
  //    this.$emit("handleItem", item);
    },

    // 拖拽成功完成
    //draggingNode当前正拖拽的节点 dropNode目标节点|参考节点 dropType拖拽到参考节点的哪个位置 ev事件对象
    handleDrop(draggingNode, dropNode, dropType, ev) {
      console.log(draggingNode, dropNode, dropType, ev);
      if (this.draggingNode) {
        // 在此处可以根据业务需求，更新数据源或者调用API进行相应的操作
        console.log("拖放完成！");
        console.log("被拖动的节点：", this.draggingNode);
        console.log("接收拖放的节点：", dropNode);
      }
      this.draggingNode = null;
    },

    // 拖拽时判定目标节点能否被放置
    allowDrop(draggingNode, dropNode, type) {
      // console.log(draggingNode.level, dropNode.level);
      //注掉的是同级拖拽
      if (draggingNode.level === dropNode.level) {
        return type === "prev" || type === "next";
      } else {
        // 不同级进行处理
        return false;
      }
    },

    allowDrag(node) {
      return true;
    },
  }
};
</script>
<style lang="scss" scoped>
.container-big {
  display: flex;
  height: calc(100vh - 100px);
  margin: 12px;

  .container-left {
    width: 278px;
    background: #fff;
    margin-right: 12px;
    padding: 20px;
    position: relative;

    .addIcon {
      position: absolute;
      right: 10px;
      top:26px;

      i {
        margin: 0px 5px;
        font-size: 16px;
        font-weight: bold;
      }
    }

    .line {
      margin: 10px -20px;
      height: 1px;
      background: #eee;

    }

    .tablist {
      height: 50px;
      display: flex;
      justify-items: center;
      border-left: none;
      border-radius: 4px 0 0 4px;
      height: 32px;
      margin-bottom: 12px;
      position: relative;

      &:after {
        content: '';
        position: absolute;
        left: -40px;
        right: -40px;
        bottom: 0px;

      }

      li {
        display: flex;
        align-items: center;
        justify-items: center;
        border-left: 1px solid #e9e9e9;
        border-radius: 4px 0 0 4px;
        font-weight: bold;
        padding: 0px 10px;
        font-size: 14px;
        color: rgba(0, 0, 0, .65);
        border: 1px solid #eee;
        cursor: pointer;

        &.active {
          border-color: #3070ff;
          color: #3070ff;
        }
      }
    }
  }

  .container-right {
    flex: 1;
    background: #fff;
  }
}


.content-big {
  height: calc(100vh - 180px);
  overflow-y: auto;

  .el-form {
    background: #fff;
    border-radius: 8px;
    margin-top: -1px;

    .el-button {
      margin-right: 10px;
    }

    .el-form-item {
      margin-bottom: 0px;

      .right-big {
        border-radius: 4px;
        border: 1px solid #DCDFE6;
        padding-left: 11px;

        > span {
          color: #777;
          width: 89px;
          display: inline-block;
          white-space: nowrap;
        }

        .el-select {
          width: 138px;

          ::v-deep .el-input__inner {
            border: none;
          }
        }

        .el-input {
          width: 138px;

          ::v-deep .el-input__inner {
            border: none;
          }
        }

        ::v-deep .el-date-editor {
          border: none;
        }
      }

    }
  }

  .content-tabel {
    background: #fff;

    .content-head {
      height: 60px;
      display: flex;
      justify-content: space-between;
      padding: 16px;

      .content-left {
        display: flex;
        align-items: center;

        .el-button {
          .el-button--medium {
            padding: 8px 20px;
          }
        }

        .left-number {
          font-size: 12px;
          color: rgba(0, 0, 0, .45);
          margin-left: 10px;
        }

        .group-btn {

        }
      }

      .content-right {
        display: flex;
        align-items: center;

        .example {
          font-size: 14px;
          display: flex;
          align-items: center;
          margin-right: 20px;

          .el-switch {
            margin-right: 10px;
            margin-left: 5px;
          }
        }
      }
    }

    .el-table {
      margin-bottom: 20px;
    }
  }

  .pagination-container {
    height: 60px;
  }
}

.table-footer {
  position: fixed;
  left: 500px;
  right: 12px;
  bottom: 0;
  height: 56px;
  padding: 12px 16px;
  border-top: 1px solid #e9e9e9;
  background: #fff;
  z-index: 2;
  display: flex;
  justify-content: space-between;

  .footer-left {
    display: flex;
    align-items: center;

    .chooseAll {
      color: #5993ff;
      margin-right: 10px;
    }

    span {
      margin: 0px 10px;
      color: #5993ff;
    }
  }

  .footer-right {
    .el-dropdown {
      margin-right: 10px;

      .el-button--primary {
        border-radius: 4px;
        color: rgba(0, 0, 0, .65);
        border: 1px solid #e9e9e9;
        background: #fff;
      }
    }
  }
}

.userInfo {
  border-radius: 4px;
  margin: 8px 0;
  cursor: pointer;
  position: relative;
  padding-left: 50px;
  font-size: 14px;
  list-style: none;
  text-align: left;

  img {
    width: 40px;
    height: 40px;
    border-radius: 8px;
    left: 0px;
    top: 0px;
    position: absolute;
  }

  .label {
    margin-top: 2px;
    font-size: 12px;
    font-family: PingFangSC-Regular, PingFang SC;
    font-weight: 400;
    color: #f90;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
}

.label-list {
  label {
    font-size: 14px;
    line-height: 1.5715;
    word-wrap: break-word;
    font-weight: 400;
  }

  span {
    margin: 4px 4px 4px 0;
    font-size: 12px;
    height: 25px;
    line-height: 25px;
    white-space: nowrap;
    background: #f0f0f0;
    border: 1px solid #e9e9e9;
    border-radius: 4px;
    padding: 0px 7px;
    display: inline-block;

  }
}
.gotoPage {
  display: flex;
  align-items: center;
  flex-direction: column;
  line-height: 30px;
  height: 400px;
  justify-content: center;
  color: rgba(0, 0, 0, .45);
  .el-button{
    margin-top: 15px;
  }
}
.search {
  display: flex;
  ::v-deep.el-input__inner {
    border-right: none !important;
    border-radius: 4px 0px 0px 0px;
  }
  ::v-deep .el-button--default {
    border-radius: 0px 4px 4px 0px;
    padding: 0px 10px;
  }
}
.dragtree {
  margin-top:10px;
  ::v-deep .el-tree-node__content {
    height: 32px;
    border-radius: 4px;
    background-color: transparent;
    padding-left: 0!important;
    &:hover,
    &:visited,
    &:focus,
    &:active {
      background-color: transparent;
    }
  }
  ::v-deep .el-tree-node__expand-icon {
    position: absolute;
    left: 28px;
    color: rgba(0,0,0,0.85);
    z-index: 1;
  }
  ::v-deep .el-tree-node__expand-icon.is-leaf{
    color: transparent;
  }
  ::v-deep .el-tree-node__children{
    .role_item{
      padding-left: 30px;
    }
  }
  // ::v-deep .el-tree-node__children .el-tree-node:hover{
  //   border-radius: 4px;
  //   background-color: #f5f5f5;
  // }
  // ::v-deep .el-tree-node__children .is-current{
  //   border-radius: 4px;
  //   background-color: #d6e8ff;
  // }
}
.role_item {
  width: 100%;
  border-radius: 4px;
  position: relative;
  height: 32px;
  padding: 0 12px;
  padding-right: 6px;
  margin-bottom: 0;
  &:hover {
    background-color: #f5f5f5;
  }
  &.active {
    background-color: #d6e8ff;

    .role-title{
      color: #3070ff;
    }
  }



  // .roleicon {
  //   display: none;
  // }

  // &:hover .roleicon {
  //   display: inline;
  // }
}
.role-title {
  width: 72%;
  white-space: nowrap;
  word-break: break-all;
  word-wrap: break-word;
  overflow: hidden;
  text-overflow: ellipsis;
  padding-right: 5px;
  padding-left: 8px;
}
</style>
